package zhengyanze.com.aipoemmax.guide.guide;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.Constant;
import zhengyanze.com.aipoemmax.guide.activity.MainActivity;
import zhengyanze.com.aipoemmax.guide.utils.AppConfigUtiles;
import zhengyanze.com.aipoemmax.guide.utils.TypefaceUtil;

/**
 * 第二种类型碎片
 * Created by MrZheng on 2017/7/20.
 */

public class GuideFragment2 extends BasLazyeFragment {

    @BindView(R.id.ll_content)
    LinearLayout llContent;
    Unbinder unbinder;
    @BindView(R.id.con1)
    TextView con1;
    @BindView(R.id.con2)
    TextView con2;
    @BindView(R.id.con3)
    TextView con3;
    @BindView(R.id.con4)
    TextView con4;

    ArrayList<View> views = new ArrayList<>();
    int index = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guide2, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        views.add(con4);
        views.add(con3);
        views.add(con2);
        views.add(con1);

        TypefaceUtil tyUtil = new TypefaceUtil(getContext(), Constant.TTF.TTF_MAIN_TYPE);
        for (View v : views) {
            v.setAlpha(0);
            tyUtil.setTypeface((TextView)v,false);
        }
    }

    @Override
    public void showAnimaort() {
        startAnimort(views.get(index));
    }

    public void startAnimort(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", new float[]{0f, 1f});
        animator.setDuration(1400);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                index++;
                if (index < views.size()) {
                    startAnimort(views.get(index));
                }
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.start)
    public void onViewClicked() {
        getActivity().startActivity(new Intent(getContext(), MainActivity.class));
        AppConfigUtiles.getAppConfigUtiles().setGuide(getContext(), false);
        getActivity().finish();
    }
}
